//
//  WBOrginalView.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/7.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
import KILabel
/// 定义统一字体的宏
let WBOrginalViewMargin:CGFloat = 10
class WBOrginalView: UIView {
    
    //更新约束的属性
    var bottonConstrain: Constraint?
    
    var orginalViewModel: WBStatusViewModel? {
        
        //赋值时调用
        didSet{
            //昵称
            nameLabel.text = orginalViewModel?.status?.user?.screen_name
            //时间
              timeLabel.text = orginalViewModel?.createat_string
            //来源
             sourceLabel.text = orginalViewModel?.source_string
            //内容
             contentLabel.attributedText = orginalViewModel?.dealAttributeString((orginalViewModel?.status?.text)!)
            //等级
            levelImageView.image = orginalViewModel?.levelImage
            //认证
            verfityImageView.image = orginalViewModel?.verifityImage
            
            //头像URL加载
            photoImageView.sd_setImageWithURL(orginalViewModel?.profile_url)
            
            //先卸载重用过来的约束
            bottonConstrain?.uninstall()
            
            if  let count = orginalViewModel?.status?.pic_urls?.count where count == 0 {
                //没有配图
                //print("原创没有配图")
                picView.hidden = true
                
                //设置底部约束为原创微博底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottonConstrain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            
            }else {  //有配图
                //print("原创有配图")
                picView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottonConstrain =  make.bottom.equalTo(picView.snp_bottom).constraint
                })
                
                //赋值
                picView.pic_urls = orginalViewModel?.status?.pic_urls
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //布局子控件
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加子控件方法
    func setupUI() {
        addSubview(photoImageView)   //头像
        addSubview(verfityImageView) //认证
        addSubview(levelImageView) //等级
        addSubview(nameLabel)    //昵称
        addSubview(timeLabel)    //时间
        addSubview(sourceLabel)  //来源
        addSubview(contentLabel) //内容
        addSubview(picView) //配图
        
        
        ///  头像
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(self.snp_top).offset(WBOrginalViewMargin)
            //宽度
            make.width.height.equalTo(50)
            
        }
        ///  认证
        verfityImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        ///  昵称
        nameLabel.snp_remakeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(WBOrginalViewMargin)
            make.top.equalTo(photoImageView.snp_top)
            
        }
        /// 等级
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(WBOrginalViewMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
            
        }
        ///  发送时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(WBOrginalViewMargin)
            make.bottom.equalTo(photoImageView.snp_bottom)
            
        }
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(WBOrginalViewMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        /// 内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(photoImageView.snp_bottom).offset(WBOrginalViewMargin)
            
        }
        
        ///  配图
        picView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            //  make.width.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            bottonConstrain = make.bottom.equalTo(picView.snp_bottom).constraint
        }
        
        
    }
    
    
    //MARK:- 懒加载
    
    /// 头像
    private lazy var photoImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    }()
    
    /// 认证
    private lazy var verfityImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    
    /// 等级
    private lazy var levelImageView:UIImageView = {
        
        let img = UIImageView()
        img.image = UIImage(named: "common_icon_membership_level1")
        return img
    }()
    
    
    
    /// 昵称
    private lazy var nameLabel:UILabel = UILabel(size:15, color: UIColor.lightGrayColor(), singleLine: true)
    /// 时间
    private   lazy var timeLabel:UILabel = UILabel(size:13, color: UIColor.orangeColor(), singleLine: true)
    ///  来源
    private lazy var sourceLabel:UILabel = UILabel(size: 13, color:UIColor.darkGrayColor() , singleLine: true)
    /// 内容
   // private lazy var contentLabel:UILabel = UILabel(size: 13, color: UIColor.blackColor(), singleLine:false)
    //改变为框架类Label
   private lazy  var contentLabel: KILabel = {
       
        let label = KILabel(size: 14, color: UIColor.blackColor(), singleLine: false)
    
    label.urlLinkTapHandler = {
         (label, string, range) in
        print(string)
    
    }
         return label
    }()
    
    ///  配图
    private lazy var picView: WBPhotoView = WBPhotoView()
    
}
